#pragma once
#include "HashTable.hpp"

namespace yiwei
{
	template<class K,class V>
	struct MapKeyOfT
	{
		const K& operator()(const std::pair<K,V>& kv)
		{
			return kv.first;
		}
	};
	template<class K,class V,class Hash = _Hash<K>>
	class unordered_map
	{
	public:
		typedef typename HashTable<K, std::pair<K, V>, MapKeyOfT<K, V>, Hash>::iterator iterator;
		typedef typename HashTable<K, std::pair<K, V>, MapKeyOfT<K, V>, Hash>::const_iterator const_iterator;

		unordered_map(){}

		unordered_map(const unordered_map& um)
			:_ht(um._ht)
		{}

		unordered_map& operator=(const unordered_map& um)
		{
			_ht = um._ht;
			return *this;
		}

		std::pair<iterator,bool> insert(const std::pair<K, V>& kv)
		{
			return _ht.Insert(kv);
		}

		V& operator[](const K& key)
		{
			std::pair<iterator, bool> ret = insert(std::make_pair(key,V()));
			return ret.first->second;
		}

		iterator begin()
		{
			return _ht.begin();
		}

		const_iterator begin() const
		{
			return _ht.begin();
		}

		iterator end()
		{
			return _ht.end();
		}

		const_iterator end() const
		{
			return _ht.end();
		}

		iterator find(const K& key)
		{
			return _ht.Find(key);
		}

		const_iterator find(const K& key) const
		{
			return _ht.Find(key);
		}

		bool erase(const K& key)
		{
			return _ht.Erase(key);
		}

		bool erase(const iterator& it)
		{
			MapKeyOfT<K, V> koft;
			return _ht.Erase(koft(*it));
		}



	private:
		HashTable<K, std::pair<K, V>, MapKeyOfT<K, V>, Hash> _ht;
	};
}